package com.dyz.leetcode;

public class MajorityElement169 {
    //摩尔投票法
    public int majorityElement(int[] nums) {
        int ret =nums[0];
        int count = 1;
        for(int i=1; i<nums.length; i++){
            if(nums[i]==ret){
                count++;
            }else {
                count--;
                if(count==0){
                    ret = nums[i+1];
                    //i+1不会越界
                    //
                }
            }
        }
        return ret;
    }
}
